//
//  WBNavigationController.swift
//  MyXinLangWeiBo
//
//  Created by 迟浩伦 on 2016/12/22.
//  Copyright © 2016年 迟浩伦. All rights reserved.
//

import UIKit

class WBNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        //隐藏navigationBar
        navigationBar.isHidden = true
    }
    //viewController是被push的控制器 设置他左侧的按钮为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        if childViewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            
            var title = "返回"
            //判断控制器的类型
            if let vc = viewController as? WBBaseViewController{
                
                //判断控制器级数
                if childViewControllers.count == 1{
                //显示首页标题
                    title = childViewControllers.first?.title ?? "返回"
                }
//                设置左侧按钮作为返回按钮
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent), isBack:true)
            }
        }
        
        
        
        super.pushViewController(viewController, animated: true)
    }
    //返回到上一级控制器
    func popToParent(){
    popViewController(animated: true)
    }
}
